package com.gitee.ixtf.vertx.internal

import io.vertx.core.Handler
import io.vertx.ext.web.RoutingContext
import jakarta.annotation.security.PermitAll
import jakarta.ws.rs.Consumes
import jakarta.ws.rs.Path
import jakarta.ws.rs.Produces
import java.lang.reflect.Method

class RestfulMethod(method: Method) : ApiMethod(method) {
  val handler: Handler<RoutingContext> by lazy {
    if (isSuspend) SuspendRestfulHandler(this) else DefaultRestfulHandler(this)
  }
  val permitAll by lazy { method.getAnnotation(PermitAll::class.java) != null }
  val consumes by lazy { method.getAnnotation(Consumes::class.java)?.value }
  val produces by lazy { method.getAnnotation(Produces::class.java)?.value }
  val route: String by lazy {
    method
        .getAnnotation(Path::class.java)
        .value
        .ifBlank { method.name }
        .removeSuffix("/")
        .removePrefix("/")
        .replace(Regex("\\{(.+?)}"), ":$1")
  }
}
